﻿using AutoMapper;
using NFine.Data.Dto.SystemManage;
using NFine.Data.Entity.SystemManage;
using NFine.Repository.SystemManage;
using NFine.Util;
using SharpRepository.Repository.Queries;
using SharpRepository.Repository.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NFine.Service.SystemManage
{
    public class UserLoginService
    {
        UserLoginRepository userLoginRepository = new UserLoginRepository(SharpRepoConfig.sharpRepoConfig, "efCore");

        public List<UserLoginGridDto> GetList(Pagination pagination, string keyword)
        {
            long? tenantId = OperatorProvider.Provider.GetOperator().TenantModel.Id;
            var specification = new Specification<UserLogin>(t => t.TenantId == tenantId);
            var pagingOptions = new PagingOptions<UserLogin, DateTime?>(pagination.page, pagination.rows, x => x.CreatedTime, isDescending: true);
            if (!string.IsNullOrEmpty(keyword))
            {
                specification = new Specification<UserLogin>(t => t.TenantId == tenantId && t.UserName.Contains(keyword));
            }
            var list = userLoginRepository.FindAll(specification, pagingOptions).ToList();
            pagination.records = pagingOptions.TotalItems;
            return Mapper.Map<List<UserLoginGridDto>>(list);
        }

        public void CreateUserLogin(UserLogin userLogin)
        {
            userLogin.Id = IdWorkerHelper.GenId64();
            userLogin.DeletedMark = false;
            userLogin.CreatedTime = DateTime.Now;
            userLoginRepository.Add(userLogin);
        }
    }
}

